package fr.inserm.umr915.vcfserver.bio.oneKgenomes;


import java.io.PrintWriter;

import org.w3c.dom.Element;

import fr.inserm.umr915.vcfserver.bio.Chromosomes;
import fr.inserm.umr915.vcfserver.util.ColumnSerializer;
import fr.inserm.umr915.vcfserver.xml.XmlStreamSerializer;
import fr.inserm.umr915.vcfserver.xml.XsdSerializer;



public class Snp1000G
	  {
	  private String chrom;
	  private int position0;
	  private String id;
	  private String ref;
	  private String alt;

	  protected Snp1000G()
	     {
		
	     }
	  
	  public String getChrom()
	     {
	     return this.chrom;
	     }
	  public int getPosition0()
	     {
	     return this.position0;
	     }
	  public String getId()
	  	{
		return id;
	  }
	 
	  public String getRef()
	     {
	     return this.ref;
	     }
	  public String getAlt()
	     {
	     return this.alt;
	     }
	  
	@Override
	  public String toString()
	     {
	     return this.getClass().toString() ;
	     }
	
	
     public static Snp1000G parseVCF(String tokens[])
        {
    	Snp1000G o=new Snp1000G();
        //o.bin=Short.parseShort(tokens[0]);
        o.chrom="chr"+tokens[0];
        o.position0=Integer.parseInt(tokens[1])-1;
        o.id=tokens[2];
        if(o.id.equals(".")) o.id=null;
        o.ref=tokens[3];
        o.alt=tokens[4];
        return o;
        }
	  
     public static class DelimitedOutputSerializer implements ColumnSerializer<Snp1000G>
     	{
    	@Override
    	public String[] getHeaders()
    		{
    		return new String[]{
    			"1000G.chrom",	
    			"1000G.position0",
    			"1000G.id",
    			"1000G.ref",
    			"1000G.alt"
    			};
    		}
    	@Override
    	public Object[] getColumns(Snp1000G t)
    		{
    		return new Object[]{
        			t.chrom,	
        			t.position0,
        			t.id,
        			t.ref,
        			t.alt
        			};
    		}
     	}
     
     
	  public static class Binding extends com.sleepycat.bind.tuple.TupleBinding<Snp1000G>
	     {
		 private Chromosomes chromosomes;
		 public Binding(Chromosomes chromosomes)
		  	{
			this.chromosomes=chromosomes;
		  	}
		 @Override
	     public Snp1000G entryToObject(com.sleepycat.bind.tuple.TupleInput in)
	        {
	    	Snp1000G o=new Snp1000G();
	    	byte b=in.readByte();
			o.chrom=this.chromosomes.getNameById(b);
			o.position0=in.readInt();
			o.id=in.readString();
			if(o.id.isEmpty()) o.id=null;
			o.ref=in.readString();
			o.alt=in.readString();
	        return o;
	        }
		 @Override
	     public void objectToEntry(Snp1000G o,com.sleepycat.bind.tuple.TupleOutput out)
	        {
			out.writeByte(this.chromosomes.getIdByName(o.chrom));
			out.writeInt(o.position0);
			out.writeString(o.id==null?"":o.id);
			out.writeString(o.ref);
			out.writeString(o.alt);
	        }
	     }
	  
	  public static class XmlSchemaSerializer
	  	extends XsdSerializer
	  		{
		  	@Override
		  	public void createBody(Element root)
		  		{
		  		Element e=root.getOwnerDocument().createElementNS(XSD, "xsd:element");
		  		root.appendChild(e);
		  		e.setAttribute("name", "Snp1000GList");
		  		
		  		Element e2= root.getOwnerDocument().createElementNS(XSD, "xsd:complexType");
		  		e.appendChild(e2);
		  		
		  		Element e3= root.getOwnerDocument().createElementNS(XSD, "xsd:sequence");
		  		e2.appendChild(e3);
		  		
		  		Element e4= root.getOwnerDocument().createElementNS(XSD, "xsd:element");
		  		e3.appendChild(e4);
		  		e4.setAttribute("name", "Snp1000G");
		  		e4.setAttribute("maxOccurs", "unbounded");
		  		
		  		
		  		
		  		}
	  		}

	  /** XMLStreamWriter serialisation */
	  public static class StaxSerializer implements XmlStreamSerializer<Snp1000G>
		  {
		  @Override
		  public void write(Snp1000G o,javax.xml.stream.XMLStreamWriter w) throws javax.xml.stream.XMLStreamException
			  {
			  w.writeStartElement("Snp1000G");
			  if(o.getId()!=null && !o.getId().isEmpty())
				  {
				  w.writeStartElement("id");
				  w.writeCharacters(String.valueOf(o.getId()));
				  w.writeEndElement();
				  }
			  w.writeStartElement("chrom");
			  w.writeCharacters(String.valueOf(o.getChrom()));
			  w.writeEndElement();
			  w.writeStartElement("position");
			  w.writeAttribute("index", "0");
			  w.writeCharacters(String.valueOf(o.getPosition0()));
			  w.writeEndElement();
			  w.writeStartElement("ref");
			  w.writeCharacters(String.valueOf(o.getRef()));
			  w.writeEndElement();
			  w.writeStartElement("alt");
			  w.writeCharacters(String.valueOf(o.getAlt()));
			  w.writeEndElement();
			  
			  w.writeEndElement();
			  }
		  }
}
